#ifndef  _REPORT_H_
#define  _REPORT_H_
#include<memory>
#include<iostream>
#include<cstdio>
#include <map>
#include <string.h>
#include"../../api/api.h"
#include "../utils/log/recoProxyLog.h"
#include "msgFactory.h"
#include "../connection/wsConnection.h"
using namespace std;
using namespace speech;
typedef std::shared_ptr<wsConnection>                sConnection;
//typedef shared_ptr<asrMsg> sMsg;
typedef shared_ptr<speech::msgFactory>                       sMsgFactory;
	
//typedef void (*cbReporter) (void *, char*);
//namespace speech
//{
class reporter
{
public:

	reporter (const char* pUuid, int iUuidLen,cbFunc pCbFunc, void* pCbObj)
		:
		_pUuid(NULL),
		_iUuidLen(iUuidLen),
		_pCbFunc(pCbFunc),
		_pCbObj(pCbObj)
	{
		if(pUuid)
		{
			_pUuid = new char [_iUuidLen +1];
			strncpy(_pUuid, pUuid,_iUuidLen);	
			_pUuid[_iUuidLen]='\0';
		}
	}
	~reporter()
	{
		if(_pUuid)
		{
			delete []_pUuid;
			_pUuid = NULL;
		}
		printf("release reporter\n");
	}
	sMsgFactory	 createMsg(int iIndex,int iMsgType)
	{
		sMsgFactory newMsg = make_shared<msgFactory> (_pCbFunc,_pCbObj);
		newMsg->createMsg(iIndex,iMsgType);
		_allMsgs.push_back(newMsg);
		return newMsg;
	}
	void setBeginTime(int iBeginTime)
	{
		_allMsgs.back()->setBeginTime(iBeginTime);	
	}
	void setFileName(const char* pFileName, int iLen)
	{
		_allMsgs.back()->setFileName(pFileName,iLen);
	}
	void updateEndTime(int iEndTime)
	{
		_allMsgs.back()->updateEndTime(iEndTime);
	}
	
	void sendMsg(int iMsgType, std::string& msg)
	{
		_allCons.back()->sendMsg(iMsgType,msg);
	}
	sConnection createCon(string url, sMsgFactory pNewMsg)
	{
		sConnection con = make_shared <wsConnection> ( url,pNewMsg);
		con->init();
		_allCons.push_back(con);
		return con;
	}

		
	char* getUuid()
	{
		return _pUuid;
	}
	int getUuidLen()
	{
		return _iUuidLen;
	}
	cbFunc getCbFunc()
	{
		return _pCbFunc;
	}
	void* getCbObj()
	{
		return _pCbObj;
	}

	void bye()
	{
		//send bye message
		//release all memory
		_allCons.clear();
		_allMsgs.clear();
		close();
	}


private:
	void close()
	{
		sMsgFactory closeMsg = createMsg(0,RECO_CB_CMDCLOSE);
		cbFunc  f = _pCbFunc;
		
        (*f)(_pCbObj,closeMsg->getMsg(),1);
		//_curMsg->reset();
	}
	char*                        _pUuid;
	int                          _iUuidLen;
	std::vector<sMsgFactory>     _allMsgs;
	std::vector<sConnection>     _allCons;
	cbFunc                       _pCbFunc;
	void*                        _pCbObj;
};

//}

#endif
